package com.dycn.controller;

import com.dycn.model.*;
import com.dycn.service.ActivitysService;
import com.dycn.service.SysRoleService;
import com.dycn.service.SysUserService;
import com.dycn.service.TaskService;
import com.dycn.util.AppResultData;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    TaskService taskService;

    @Autowired
    ActivitysService activitysService;

    @GetMapping("listUser.json")
    @ResponseBody
    public AppResultData listUser(int page){
        return  sysUserService.listUser(page);
    }
    @GetMapping("listUser")
    public String listUser(){
        return  "role/list";
    }

    @PostMapping("saveOrUpdate.json")
    @ResponseBody
    public AppResultData addUser(SysUser sysUser,Integer roleId){
        return  sysUserService.saveOrUpdate(sysUser,roleId);
    }
    @GetMapping("modifyUser")
    public String modifyUser(Model model, String userId){
        SysUser sysUser=sysUserService.findById(userId);
        model.addAttribute("user",sysUser);
        return  "role/adduser";
    }
    @GetMapping("modify")
    public String modify(Model model, String roleId){

        model.addAttribute("roleId",roleId);
        return  "role/modify";
    }

    @GetMapping("listroleuser")
    public String listroleuser(){
        return  "role/listroleuser";
    }
    @GetMapping("listroleuser.json")
    @ResponseBody
    public AppResultData listroleuser(int page){
        return  sysRoleService.list(page);
    }

    @GetMapping("findRoleVo.json")
    @ResponseBody
    public AppResultData findRoleVo(String roleId){
        return  sysRoleService.findRoleVo(roleId);
    }

    @PostMapping("save.json")
    @ResponseBody
    public AppResultData save(Integer roleId,String ids){
        return  sysRoleService.save(roleId,ids);
    }

    @GetMapping("getTask")
    @ResponseBody
    public  AppResultData  getTask(int page,String type,Integer activityId){
        String username = SecurityUtils.getSubject().getPrincipal().toString();
        return  sysUserService.listTask(page,username,type,activityId);
    }

    @GetMapping("getSigns")
    @ResponseBody
    public  AppResultData  getSigns(int page,Integer activityId){
        return  taskService.listSigns(page,activityId);
    }

    @GetMapping("listUserSign")
    @ResponseBody
    public  AppResultData  listUserSign(@Param("openid") String openid, @Param("activityId") Integer activityId, int page){
        return  taskService.listUserSign(openid,activityId,page);
    }

    @GetMapping("zhuliList")
    @ResponseBody
    public  AppResultData  zhuliList(int page,@Param("activityId") Integer activityId){
        return  taskService.zhuliList(page,activityId);
    }

    @GetMapping("listUserZhuli")
    @ResponseBody
    public  AppResultData  listUserZhuli(String taskId,int page){
        return  taskService.listZhuli(page,taskId);
    }


    @GetMapping("getDates")
    @ResponseBody
    public  AppResultData  getDates(String type){
        return  taskService.getDates(type);
    }

    @PostMapping("saveDate")
    @ResponseBody
    public  AppResultData  saveDate(@RequestParam("type") String type, @RequestParam("startDate")String startDate,@RequestParam("endDate") String endDate){
        return  taskService.saveDate(type,startDate,endDate);
    }
    @GetMapping("getZhuli")
    @ResponseBody
    public  AppResultData  getZhuli(@Param("activityId") Integer activityId){
        return  taskService.getZhuli(activityId);
    }
    @PostMapping("saveZhuli")
    @ResponseBody
    public  AppResultData  saveZhuli(@RequestParam("title") String title, @RequestParam("content")String content){
        return  taskService.saveZhuli(title,content);
    }

    @GetMapping("getXuan")
    @ResponseBody
    public  AppResultData  getXuan(Integer id){
        Activitys activitys=activitysService.selectByPrimaryKey(id);
        if(activitys!=null)
        return  AppResultData.success("成功",activitys.toActivitysVo());
        else {
            return  AppResultData.success("成功",null);
        }
    }
    @PostMapping("saveXuan")
    @ResponseBody
    public  AppResultData  saveXuan(ActivitysVo activitysVo){
        return  activitysService.saveOrUpdate(activitysVo.toActivitys());
    }
}
